<template>
	<!-- 优惠券添加 -->
	<div>
		<div class="edit-top">
			添加信息
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-title">优惠券信息</div>
			<div class="edit-item">
				<label>优惠券名称：</label><input v-model="info.name" type="text" />
			</div>
			<div class="edit-item">
				<label>面额：</label><input v-model="info.amount" type="number" />
			</div>
			<div class="edit-item">
				<label>优惠码：</label><input v-model="info.code" type="text" />
			</div>
			<div class="edit-item">
				<label>发行数量</label><input v-model="info.publishCount" type="number" />
			</div>
			
			<div class="edit-item">
				<label>使用门槛：满</label><input v-model="info.minPoint" type="number" />
			</div>
			<div class="edit-item">
				<label>使用类型</label><select v-model="info.useType">
					<option v-for='item in dict.useType' :value="item.value" :key="item.value">{{item.type}}</option>
				</select>
			</div>
			<div class="edit-item">
				<label>每人限领：</label><input v-model="info.perLimit" type="number" />
			</div>
			<div class="edit-item">
				<label>开始领取时间：</label><el-date-picker
				      v-model="info.enableTime"
				      type="datetime"
					  value-format="yyyy-MM-dd HH:mm:ss"
				      placeholder="选择开始时间">
				 </el-date-picker>
			</div>
			<div class="edit-item">
				<label>生效时间：</label><el-date-picker
				     v-model="info.startTime"
				     type="datetime"
					 value-format="yyyy-MM-dd HH:mm:ss"
				     placeholder="选择开始时间">
				</el-date-picker>
			</div>
			<div class="edit-item">
				<label>结束时间：</label><el-date-picker
				     v-model="info.endTime"
				     type="datetime"
					 value-format="yyyy-MM-dd HH:mm:ss"
				     placeholder="选择结束时间">
				</el-date-picker>
			</div>
			<div class="edit-item" style="display:flex">
				<label>备注：</label>
				<textarea name="" id="" rows="6" v-model="info.note"></textarea>
			</div>
			
		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' type="primary">保存</el-button>
		</div>
	</div>
</template>

<script>
	import Global from '@/components/Global.vue'
	export default {
		data() {
			return {
				dict:'',
				dep_list:[],
				info: {
					name:'',
					amount:'',
					code:'',
					publishCount:'',
					minPoint:'',
					useType:0,
					perLimit:'',
					enableTime:'',
					endTime:'',
					startTime:'',
					note:''
				}
			}
		},
		mounted() {
			this.get_dict();
		},
		methods: {
			get_dict() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=coupon')
				  .then(function (res) {
					  if(res != '')
						self.dict = res.data;
				  })
			},
			save() {
				if (this.info.name == "") {
					this.$popup.open('优惠券名称不能为空');
					return;
				}
				if (this.info.amount == "") {
					this.$popup.open('面额不能为空');
					return;
				}
				if (this.info.code == "") {
					this.$popup.open('优惠码不能为空');
					return;
				}
				if (this.info.minPoint == "") {
					this.$popup.open('使用门槛不能为空');
					return;
				}
				if (this.info.perLimit == "") {
					this.$popup.open('每人限领张数不能为空');
					return;
				}
				if (this.info.publishCount == "") {
					this.$popup.open('发行数量不能为空');
					return;
				}
				if (this.info.enableTime == "") {
					this.$popup.open('可领取日期不能为空');
					return;
				}
				if (this.info.startTime == "") {
					this.$popup.open('开始时间不能为空');
					return;
				}
				if (this.info.endTime == "") {
					this.$popup.open('结束时间不能为空');
					return;
				}
				var self = this;
				this.$axios.post(this.$global.serverIp + 'frontend/coupon/save', self.info)
					.then(function(res) {
						if (res != '') {
							self.success('添加成功');
						}
					})
			},
			success(content) {
				var self = this;
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						self.$emit("saveAdd", 2);
					}
				});
			},
			
			cancel() {
				this.$emit("saveAdd", 1);
			}
		}
	}
	import '@/assets/css/edit.css'
</script>
